﻿using System.Windows;
using System.Windows.Controls;

using YourLastOptionsDialog.Core.Attributes;

namespace YourLastOptionsDialog.Views
{
    /// <summary>
    /// The editor for date time values.
    /// </summary>
    public partial class DateTimeEditor : UserControl
    {
        /// <summary>
        /// The date time mode property.
        /// </summary>
        public static readonly DependencyProperty DateTimeModeProperty =
            DependencyProperty.Register(
                "DateTimeMode", 
                typeof(DateTimeMode), 
                typeof(DateTimeEditor), 
                new PropertyMetadata(default(DateTimeMode), DateTimeMode_Changed));

        private static void DateTimeMode_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = d as DateTimeEditor;
            if (obj != null)
            {
                obj.SetPickerVisibilities();
            }
        }

        /// <summary>
        /// Gets or sets the date time mode.
        /// </summary>
        public DateTimeMode DateTimeMode
        {
            get
            {
                return (DateTimeMode)GetValue(DateTimeModeProperty);
            }

            set
            {
                SetValue(DateTimeModeProperty, value);
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DateTimeEditor"/> class.
        /// </summary>
        public DateTimeEditor()
        {
            // Required to initialize variables
            InitializeComponent();
            SetPickerVisibilities();
        }

        private void SetPickerVisibilities()
        {
            switch (DateTimeMode)
            {
                case DateTimeMode.Date:
                    DatePicker.Visibility = Visibility.Visible;
                    DatePicker.Margin = new Thickness(0);
                    TimePicker.Visibility = Visibility.Collapsed;
                    break;
                case DateTimeMode.Time:
                    DatePicker.Visibility = Visibility.Collapsed;
                    TimePicker.Visibility = Visibility.Visible;
                    TimePicker.Margin = new Thickness(0);
                    break;
                case DateTimeMode.DateTime:
                    DatePicker.Visibility = Visibility.Visible;
                    DatePicker.Margin = new Thickness(0, 0, 0, -6);
                    TimePicker.Visibility = Visibility.Visible;
                    TimePicker.Margin = new Thickness(0, -6, 0, 0);
                    break;
            }
        }
    }
}